//
//  AppDelegate.m
//  PCAForWar
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "VC2ViewController.h"
#import "VC3.h"
#import "VC4.h"
#import "VCForMySelf.h"
#import "VCForSearch.h"
#import "FirstView.h"
#import "DataBaseHandler.h"

@interface AppDelegate ()

@property (nonatomic, retain) UITabBarController *tb;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    self.window.backgroundColor = [UIColor whiteColor];
    
    // 启动页睡眠时间
    [NSThread sleepForTimeInterval:1.0];
    
    [self createTabBarController];
    
    ViewController *vc = [[ViewController alloc]init];
    
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:vc];
    
    self.window.rootViewController = navi;
    
    self.window.rootViewController = self.tb;
    
    [self.window makeKeyAndVisible];
    
    [self loadDataBase];
    
    // 引导页
    if (![[NSUserDefaults standardUserDefaults]boolForKey:@"Every"]) {
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Every"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"First"];
        
    }
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"First"] == YES) {
        
        FirstView *first = [[FirstView alloc] initWithFrame:self.window.frame];
        
        first.tag = 100;
        
        [first.button addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.window addSubview:first];
        
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"First"];
        
    }
    
    
    
    [navi release];
    [vc release];
    [_window release];
    
    return YES;
}

#pragma mark 创建数据库
- (void)loadDataBase {
    
    [[DataBaseHandler shareDataBaseHandler] open];
    [[DataBaseHandler shareDataBaseHandler] createTable];
    
    NSLog(@"%@", NSHomeDirectory());
    
}

#pragma mark 引导页点击方法
- (void)nextAction:(UIButton *) button {
    
    FirstView *first = [self.window viewWithTag:100];
    
    [first removeFromSuperview];
    
}

#pragma mark 创建tableBar
- (void)createTabBarController {
    
    self.tb = [[UITabBarController alloc] init];
    
    [self createViewControllers];
    
    self.tb.tabBar.tintColor = [UIColor whiteColor];
    self.tb.tabBar.barTintColor = [UIColor redColor];
    
    self.window.rootViewController = self.tb;
    
    
    [self.tb release];
}

#pragma mark 创建tabBar
- (void)createViewControllers {
    
    ViewController *vc = [[ViewController alloc]init];
    
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:vc];
    
    navi.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[[UIImage imageNamed:@"Shouye"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:100];
    
    
    VC2ViewController *vc2 = [[VC2ViewController alloc] init];
    UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:vc2];
    
    navi2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"军评" image:[[UIImage imageNamed:@"JunPing"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] tag:200];
    
    
    VC3 *vc3 = [[VC3 alloc] init];
    UINavigationController *navi3 = [[UINavigationController alloc] initWithRootViewController:vc3];
    
    navi3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"图库" image:[[UIImage imageNamed:@"TuKu"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] tag:300];
    
    VC4 *vc4 = [[VC4 alloc] init];
    UINavigationController *navi4 = [[UINavigationController alloc] initWithRootViewController:vc4];
    
    navi4.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"问吧" image:[[UIImage imageNamed:@"WenBa"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] tag:400];
    
    VCForSearch *vcSearch = [[VCForSearch alloc] init];
    
    UINavigationController *navi5 = [[UINavigationController alloc] initWithRootViewController:vcSearch];
    
    navi5.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"搜索" image:[[UIImage imageNamed:@"sousuo"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] tag:500];
    
    self.tb.viewControllers = @[navi,navi2,navi3,navi4,navi5];
    
    [vc release];
    [vc2 release];
    [vc3 release];
    [vc4 release];
    [vcSearch release];
    [navi release];
    [navi2 release];
    [navi3 release];
    [navi4 release];
}

- (void)dealloc{
    
    [_tb release];
    [_window release];
    [super dealloc];
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
